package com.szqhyxj.youxiangju.biz.partnerHome.shop.order;

import com.szqhyxj.youxiangju.biz.merchantHome.shop.OrderList;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.order.OrderContract;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;
import com.szqhyxj.youxiangju.sys.BaseResult;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class SubordinateOrderPresenter extends AbBasePresenter implements OrderContract.Presenter {

    private final SubordinateOrderContract.View view;

    public SubordinateOrderPresenter(SubordinateOrderContract.View view){
        this.view = view;
    }


    @Override
    public void init(){
        addDisposables(SubordinatePartnerOrderModel.getOrderList()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
        .subscribe(new Consumer<BaseResult<OrderList>>(){
            @Override
            public void accept(BaseResult<OrderList> orderListBaseResult) throws Exception{
                view.dismissLoading();
                if(orderListBaseResult.businessLogicSuccess()){
                    if(orderListBaseResult.getData().getRows().size()>0){
                        view.showOrderList(orderListBaseResult.getData().getRows());
                    }else {
                        view.showNoOrder();
                    }
                }else {
                    view.showLoading(orderListBaseResult.getMessage());
                    view.showNoOrder();
                }
            }
        }, new Consumer<Throwable>(){
            @Override
            public void accept(Throwable throwable) throws Exception{
                view.showLoading(throwable.getMessage());
                view.dismissLoading();
                view.showNoOrder();
            }
        }));
    }

    @Override
    public void nowConfirm(final int i, String orderId){
        view.navConfirmOrder(orderId);
    }

    @Override
    public void addDisposables(Disposable disposable){
        super.addDisposables(disposable);
        view.showLoading();
    }
}